package com.lfs.Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lfs.Entity.ProductEntity;



public class JDBCUtil {
         private static final String DRIVER="com.mysql.cj.jdbc.Driver";
         private static final String URL="jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
         private static final String NAME="root";
         private static final String PWD="root";
         
         static {
        	 try {
				Class.forName(DRIVER);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
        	 
         }
        public static Connection getconnection() {
        	try {
			return	DriverManager.getConnection(URL,NAME,PWD);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
        	
        }
        public static PreparedStatement getpreparedStatement(String sql,Object...objects) {
 	  	   try {
 				PreparedStatement prepareStatement = getconnection().prepareStatement(sql);
 				for (int i = 0; i < objects.length; i++) {
 					prepareStatement.setObject(i+1, objects[i]);
 				}return prepareStatement;
 			} catch (SQLException e) {
 				e.printStackTrace();
 			}
 			return null;
 	    }

        public static boolean executeUpade(String sql,Object...objects) {
			   try {
				int updae = getpreparedStatement(sql, objects).executeUpdate();
				if(updae>0) return true;
			} catch (SQLException e) {
				e.printStackTrace();
			}
	   	  return false;
	     }

        public static List<ProductEntity> executeQuery(String sql,Object...objects) {
         	 List<ProductEntity> roderList = new ArrayList<ProductEntity>();
       		   try {
       			ResultSet resultSet = getpreparedStatement(sql, objects).executeQuery();
       		    while (resultSet.next()) {
       		    	ProductEntity roder = new ProductEntity(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),resultSet.getInt(4),resultSet.getDouble(5),resultSet.getString(6));
       		    	roderList.add(roder);
       		    }
       		} catch (SQLException e) {
       			e.printStackTrace();
       		}
         	  return roderList;
           }
           
}


